package cn.skyquant.quant4j.service.boot.web.controller;

import cn.skyquant.quant4j.api.dto.ResultDTO;
import cn.skyquant.quant4j.api.ylzc.YlzcDTO;
import cn.skyquant.quant4j.api.ylzc.YlzcMap;
import cn.skyquant.quant4j.api.ylzc.YlzcService;
import cn.skyquant.quant4j.sdk.enums.YlzcType;
import cn.skyquant.quant4j.sdk.util.time.CalendarUtils;
import cn.skyquant.quant4j.service.boot.model.authority.RoleAuthority;
import cn.skyquant.quant4j.service.boot.model.ylzc.Ylzc;
import cn.skyquant.quant4j.service.boot.model.ylzc.YlzcRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;

@RestController
@RequestMapping("/ylzc")
@RoleAuthority(roleNames = {RoleAuthority.administrator,RoleAuthority.client})
public class YlzcController {

    @Autowired
    YlzcService ylzcService;
    @Autowired
    YlzcRepository ylzcRepository;

    @RequestMapping(value = "",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO<Void> insert(
            @RequestParam(value = "symbol",required = true) String symbol,
            @RequestParam(value = "period",required = true) Integer period,
            @RequestParam(value = "thedate",required = true) String thedate,
            @RequestParam(value = "type",required = true) YlzcType type,
            @RequestParam(value = "level",required = true) int level,
            @RequestParam(value = "value",required = true) double value) {

        Date date = null;
        try {
            date = CalendarUtils.parseOption(thedate);
        } catch (ParseException e) {
            return ResultDTO.error("新增压力支撑位失败,日期格式错误:" + thedate);
        }
        return ylzcService.insert(symbol, period, date, type, level, value);
    }


    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO<YlzcDTO> get(@PathVariable(name = "id",required = true) Integer id) {
        return ylzcService.get(id);
    }

    @RequestMapping(value = "/{id}/active",method = RequestMethod.PUT)
    @ResponseBody
    public ResultDTO<Void> active(@PathVariable(name = "id",required = true) Integer id) {
        return ylzcService.active(id);
    }

    @RequestMapping(value = "/{id}/disable",method = RequestMethod.PUT)
    @ResponseBody
    public ResultDTO<Void> disable(@PathVariable(name = "id",required = true) Integer id) {
        return ylzcService.disable(id);
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    @ResponseBody
    public ResultDTO<Void> update(
            @PathVariable(name = "id",required = true) Integer id,
            @RequestParam(value = "level",required = true) int level,
            @RequestParam(value = "value",required = true) double value) {
        return ylzcService.update(id, level,value);
    }

    //找到指定时间所有的压力位和支撑位 app端使用
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO<Page<Ylzc>> findByPage(
            @RequestParam(value = "symbol",required = true) String symbol,
            @RequestParam(value = "period",required = true) Integer period,
            @RequestParam(value = "level",required = true) Integer level,
            @RequestParam(value = "type",required = true) YlzcType type,
            @PageableDefault(value = 15, sort = { "thedate" }, direction = Sort.Direction.DESC) Pageable pageable){
        if(level==0){
            Page<Ylzc> pageYlzc = ylzcRepository.findBySymbolAndPeriodAndType(symbol,period,type,pageable);
            return ResultDTO.success(pageYlzc);
        }else {
            Page<Ylzc> pageYlzc = ylzcRepository.findBySymbolAndPeriodAndTypeAndLevel(symbol,period,type,level,pageable);
            return ResultDTO.success(pageYlzc);
        }
    }

    //找到指定时间所有的压力位和支撑位 app端使用
    @RequestMapping(value = "",method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO<YlzcMap> query(
            @RequestParam(value = "symbol",required = true) String symbol,
            @RequestParam(value = "period",required = true) Integer period,
            @RequestParam(value = "begin",required = true) long begin,
            @RequestParam(value = "end",required = true) long end){
        return ylzcService.query(symbol,period,new Date(begin),new Date(end));
    }
}
